Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public NotInheritable Class Misc
	Private Sub New()
	End Sub
	Shared Sub CleanUpRef(ByVal o As Object)
		If o Is Nothing Then Exit Sub
		Dim ii As Integer
		Do
			ii = Marshal.ReleaseComObject(o)
		Loop Until ii <= 0
		o = Nothing
	End Sub

    Public Shared Sub EnableVisualStyles(ByVal f As Form)
        If IsThemesSupported AndAlso System.IO.File.Exists((Application.ExecutablePath + ".manifest")) Then
            RecursivelyFormatForWinXP(f)
        End If
    End Sub

    Public Shared Sub RecursivelyFormatForWinXP(ByVal control As Control)
        Dim x As Integer
        For x = 0 To control.Controls.Count - 1
            ' If the control derives from ButtonBase, 
            ' set its FlatStyle property to FlatStyle.System.
            If control.Controls(x).GetType().BaseType Is GetType(ButtonBase) Then
                CType(control.Controls(x), ButtonBase).FlatStyle = FlatStyle.System
            End If

            ' If the control holds other controls, iterate through them also.
            If control.Controls.Count > 0 Then
                RecursivelyFormatForWinXP(control.Controls(x))
            End If
        Next x
    End Sub


    Public Shared ReadOnly Property IsWindowsXP() As Boolean
        Get
            If Environment.OSVersion.Platform = PlatformID.Win32NT AndAlso _
                        Environment.OSVersion.Version.Major >= 5 AndAlso _
                        Environment.OSVersion.Version.Minor > 0 Then
                Return True
            Else
                Return False
            End If

        End Get
    End Property


    Public Shared ReadOnly Property IsThemesSupported() As Boolean
        Get
            If IsWindowsXP AndAlso OSFeature.Feature.IsPresent(OSFeature.Themes) Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property

End Class
